<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get_detail';
if($_RQ['op']=='get_detail'){
	$return['ischecklogin']=cfc('core')->checklogin('','',true);
	$id=$_RQ['id'];
	$item=pdo_get('jy_msgorder_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$item['images']=json_decode($item['images'],true);
	$item['onlinetime']=json_decode($item['onlinetime'],true);
	$item['priceset']=json_decode($item['priceset'],true);
	$item['param']=json_decode($item['param'],true);
	$item['zixuns']=json_decode($item['zixuns'],true);
	$item['peixuns']=json_decode($item['peixuns'],true);
	$item['tags']=json_decode($item['tags'],true);
	$item['api_data']=json_decode($item['api_data'],true);
	$item['salenum']=pdo_count('jy_msgorder_tcp',array('uniacid'=>$_SESSION['uniacid'],'workerid'=>$item['id']));
	//来自海报
	$jy_poster = $this->plugin_exists('jy_poster');
	if($jy_poster  && $return['setting']['no_poster']!=1){
		$item['isposter']=1;
	}
	$item['showprice']=$item['priceset'][0]['price'];
	$item['logo']=tomedia($item['logo']);
	$return['goods']=$item;
	$return['post']['goodsid']=$item['id'];
	$return['post']['pricekey']=0;
	$return['zxout_time']=plugin('jy_weishop')->model('store')->zxout_time();
	$return['xieyi']=$this->plugin_setting('xieyi');
	$return['goods']['imagetop']=$return['goods']['images'][0]==$return['goods']['image']?$return['goods']['images'][1]:$return['goods']['images'][0];
	$return['sharedata']['link']=custom_url('jy_msgorder/goods.detail//id='.$return['goods']['id'].'&fromuser='.$_SESSION['uid'].'&onemerchid='.$_SESSION['onemerchid'],true);
	$keywords=$return['goods']['share_keywords']?:$return['goods']['title'].','.$return['sharedata']['desc'];
	$desc=$return['sharedata']['desc'];
	set_html('title',$return['goods']['title']?$return['goods']['title'].'-'.$return['shopset']['title']:'专家详情');
	set_html('keywords',$keywords);
	set_html('description',$desc);
	exi($return);
}
if($_RQ['op']=='upposter'){
	$goodsid=intval($_RQ['goodsid']);
	//小程序二维码：
	$url='/jy_msgorder/goods/detail/index?id='.$goodsid.'&fromuser='.$_SESSION['uid'];
	$pid='wxappnew'.$_SESSION['wxapp_id'].'_'.$goodsid.'_'.$_SESSION['uid'];
	if($_SESSION['fromh5wxapp']){
		$h5url='jy_msgorder/goods.detail//id='.$goodsid.'&fromuser='.$_SESSION['uid'];
		$url='/pages/index/index?url='.urlencode($h5url);
	}
	//$appcode = $this->model('model')->get_wxappewm($url,$pid);
	//海报文件
	if($_SESSION['fromh5wxapp'] || $_SESSION['container']=='wxapp'){
		$poster=model('jy_poster/model')->get_active(3,'',$goodsid,$_SESSION['openid'],$appcode,'','jy_msgorder','',true);
	}else{
		$poster=model('jy_poster/model')->get_active(3,'',$goodsid,$_SESSION['openid'],'','','jy_msgorder','',true);
	}
	$poster['posterimg']=$poster['poster'];
	exi($poster);
}
if($_RQ['op']=='getcomments'){
	$goodsid=$_RQ['goodsid']?intval($_RQ['goodsid']):$_SESSION['goodsid'];
	$return['list']=$this->model('goods')->get_comments($goodsid,6,$comment_total,$_RQ['type'],$_RQ['page']);
	exi($return);
}
if($_RQ['op']=='check_buy'){
	$postdata=get_postdata();
	$type = $_RQ['type'];
	$goodsid = $_RQ['goodsid'];
	$pricekey = intval($_RQ['pricekey']);
	//$buyxieyi=$this->plugin_setting('xieyi');
	if(!$postdata['checked'] && $buyxieyi['isopen']){
		//	exi('请先阅读协议！','error');
	} 
	if(!$goodsid){
		exi('请选择专家！','error');
	}
	$item=pdo_get('jy_msgorder_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goodsid));
	$item['priceset']=json_decode($item['priceset'],true);
	if(!$type){
		$price=$item['price'];
		$goodsinfo=$item['title'].'-在线咨询';
	}elseif($type==1){
		$price=$item['mobileprice'];
		$goodsinfo=$item['title'].'-电话咨询';
	}elseif($type==2){
		$price=$item['priceset'][$pricekey]['price'];
		$goodsinfo='[咨询套餐]'.$item['title'];
	}
	$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'type'=>$type,
			'workerid'=>$item['id'],
			'groupid'=>$item['groupid'],
			'price'=>$price,
			'goodsinfo'=>$goodsinfo,
			'ordersn'=>get_ordersn('jy_msgorder_tcp','ordersn'),
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_msgorder_tcp',$insert);
		$id=pdo_insertid();
		if($price>0){
			$params=array(
					'id'=>$id,
					'title'=>'检测订单',
					'fee'=>$price,
					'plugin'=>'jy_msgorder',
					//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
					'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
					'back_url'=>'jy_msgorder/goods.detail/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
					'success_url'=>'jy_msgorder/mytcp/',
					'error_url'=>'jy_msgorder/mytcp/',
			);
			$payid=cfc('pay')->create_pay_order($params);
			$this->pdo_update('tcp',array('pay_id'=>$payid),array('id'=>$id));
			$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
			);
			exi($res);
		}else{
			$payresult=array('fee'=>0);
			$order=$insert;
			$order['id']=$id;
			$this->model('model')->payresult($order);
			$this->model('notice')->notice_fuwuorder($order);	
			exi('提交成功！','','jy_msgorder/mytcp/');
		}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_msgorder_tcp',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	$this->model('model')->payresult($order,$payresult,1);
	$this->model('notice')->notice_fuwuorder($order);	
	exi('支付成功','success','jy_msgorder_tcp/tcp/list/status=1&id='.$order['id']);
}